package com.xuzhiguang.baidu.pan.sdk;

import com.xuzhiguang.baidu.pan.sdk.common.http.HttpClient;
import com.xuzhiguang.baidu.pan.sdk.configuration.BaiduPanConfiguration;
import com.xuzhiguang.baidu.pan.sdk.operations.BaiduPanBasicOperations;
import com.xuzhiguang.baidu.pan.sdk.operations.BaiduPanOAuthOperations;
import com.xuzhiguang.baidu.pan.sdk.operations.BaiduPanOperations;
import com.xuzhiguang.baidu.pan.sdk.operations.BaiduPanUploadOperations;
import com.xuzhiguang.baidu.pan.sdk.token.ITokenStorage;
import com.xuzhiguang.baidu.pan.sdk.token.InMemoryTokenStorage;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @author xuzhiguang
 * @date 2020/6/25 17:00
 */
public class BaiduPanClient implements BaiduPan {

    private BaiduPanOperations baiduPanOperations;

    private BaiduPanOAuthOperations baiduPanOAuthOperations;

    private BaiduPanBasicOperations baiduPanBasicOperations;

    private BaiduPanUploadOperations baiduPanUploadOperations;

    private HttpClient httpClient;

    private ITokenStorage tokenStorage;

    private BaiduPanConfiguration baiduPanConfiguration;

    private BaiduPanClient() {}


    /**
     * 基本操作
     *
     * @return
     */
    @Override
    public BaiduPanOperations ops() {
        return this.baiduPanOperations;
    }

    /**
     * 基础能力接口
     *
     * @return
     */
    @Override
    public BaiduPanBasicOperations basicOps() {
        return this.baiduPanBasicOperations;
    }

    /**
     * 百度api鉴权接口
     *
     * @return
     */
    @Override
    public BaiduPanOAuthOperations oauthOps() {
        return this.baiduPanOAuthOperations;
    }

    /**
     * 上传文件接口
     *
     * @return
     */
    @Override
    public BaiduPanUploadOperations uploadOps() {
        return this.baiduPanUploadOperations;
    }


    public static class Builder {

        private BaiduPanClient baiduPanClient;

        public Builder() {
            baiduPanClient = new BaiduPanClient();
        }

        /**
         * 鉴权设置
         * @param apiKey
         * @param apiSecret
         * @return
         */
        public Builder auth(String apiKey, String apiSecret) {

            if (StringUtils.isEmpty(apiKey)) {
                throw new IllegalArgumentException("参数 apiKey 不能为空");
            }

            if (StringUtils.isEmpty(apiSecret)) {
                throw new IllegalArgumentException("参数 apiSecret 不能为空");
            }

            baiduPanClient.baiduPanConfiguration = new BaiduPanConfiguration(apiKey, apiSecret);
            return this;
        }

        /**
         * token存储设置
         * @param tokenStorage
         * @return
         */
        public Builder tokenStorage(ITokenStorage tokenStorage) {
            baiduPanClient.tokenStorage = tokenStorage;
            return this;
        }

        /**
         * 超时设置
         * @param connectTimeout
         * @param readTimeout
         * @return
         */
        public Builder timeout(long connectTimeout, long readTimeout) {

            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
                    .readTimeout(readTimeout, TimeUnit.MILLISECONDS)
                    .build();
            baiduPanClient.httpClient = new HttpClient(okHttpClient);
            return this;
        }

        /**
         * build
         * @return
         */
        public BaiduPanClient build() {
            if (baiduPanClient.baiduPanConfiguration == null) {
                throw new IllegalArgumentException("请设置auth相关参数");
            }

            if (baiduPanClient.httpClient == null) {
                baiduPanClient.httpClient = new HttpClient(new OkHttpClient());
            }

            if (baiduPanClient.tokenStorage == null) {
                baiduPanClient.tokenStorage = new InMemoryTokenStorage();
            }

            initOps();

            return baiduPanClient;
        }

        private void initOps() {
            baiduPanClient.baiduPanOperations = new BaiduPanOperations(baiduPanClient.httpClient,
                    baiduPanClient.tokenStorage, baiduPanClient.baiduPanConfiguration);

            baiduPanClient.baiduPanOAuthOperations = new BaiduPanOAuthOperations(baiduPanClient.httpClient,
                    baiduPanClient.tokenStorage, baiduPanClient.baiduPanConfiguration);

            baiduPanClient.baiduPanBasicOperations = new BaiduPanBasicOperations(baiduPanClient.httpClient,
                    baiduPanClient.tokenStorage, baiduPanClient.baiduPanConfiguration);

            baiduPanClient.baiduPanUploadOperations = new BaiduPanUploadOperations(baiduPanClient.httpClient,
                    baiduPanClient.tokenStorage, baiduPanClient.baiduPanConfiguration);
        }
    }
}
